var twbOrderSupplierVm = new Vue({
    el:'#twbOrderSupplierApp',
    data:{
        page: {currentPage: 0, pageSizes: [20, 50, 100], pageSize: 20, total: 0, ascending: [], descending: []},
        twbOrderSupplierData: [],
        suppStatistics: {},
        q:{
            proStatus: 1,
            finStatus: 1,
            orderNo: null,
            producerName: null,
            createTime: null,
            beginCreatetime: null,
            endCreatetime: null,
        },
        /* 批量付款 */
        suppPayFormVisible: false,
        suppPayForm: {
            productionIds: [],
            financePaytime: null,
            financeRemark: null,
        },
    },
    mounted: function(){
        this.query();
    },
    methods: {
        search: function () {
            this.page.currentPage = 0;
            this.query();
        },
        query: function () {
            var self = this;
            var page = self.page;
            var queryParam = self.q;
            queryParam.page = page.currentPage;
            queryParam.limit = page.pageSize;
            // 录入时间
            var createTime = queryParam.createTime;
            if (!isBlank(createTime)){
                queryParam.beginCreatetime = createTime[0] + ' 00:00:00';
                queryParam.endCreatetime = createTime[1] + ' 23:59:59';
            }
            $.ajax({
                type: "POST",
                url: baseURL + "order/erporder/queryOrderSupplierList",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(queryParam),
                success: function (r) {
                    if (r.code == 0) {
                        self.suppStatistics = r.suppStatistics;
                        self.page.total = r.page.totalCount;
                        self.twbOrderSupplierData = r.page.list;
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        // 批量付款弹窗
        batchPaymentDialog: function () {
            var selected = this.$refs.twbOrderSupplierTable.selection;
            if (selected.length == 0) {
                this.$message.error('请选择外协单号 !');
                return;
            }
            var notPayNum = 0;
            this.suppPayForm.productionIds = [];
            for (var s in selected){
                var item = selected[s];
                var financeStatus = item.financeStatus;
                if (financeStatus != 3){
                    notPayNum += 1;
                    this.suppPayForm.productionIds.push(selected[s].productionId);
                }
            }
            if (notPayNum == 0){
                this.$message.error('请选择未付款的外协单号 !');
                return;
            }
            this.suppPayFormVisible = true;
        },
        // 批量付款
        batchPayment: function () {
            var self = this;
            var financePaytime = self.suppPayForm.financePaytime;
            if (isBlank(financePaytime)){
                financePaytime = new Date();
            }
            var twbLogisticsPaymentDto = {
                productionIds: self.suppPayForm.productionIds,
                financePaytime: self.dateFormat(financePaytime),
                financeRemark: self.suppPayForm.financeRemark
            }
            $.ajax({
                type: "POST",
                url: baseURL + "order/erporder/twbOrderSuppPayment",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(twbLogisticsPaymentDto),
                success: function (r) {
                    if (r.code == 0) {
                        self.suppPayFormVisible = false;
                        self.query();
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        // 批量收货
        batchReceiveGoods: function () {
            var selected = this.$refs.twbOrderSupplierTable.selection;
            if (selected.length == 0) {
                this.$message.error('请选择外协单号 !');
                return;
            }
            var notFinishNum = 0;
            var productionIds = [];
            for (var s in selected){
                var item = selected[s];
                var productionStatus = item.productionStatus;
                if (productionStatus != 3){
                    notFinishNum += 1;
                    productionIds.push(selected[s].productionId);
                }
            }
            if (notFinishNum == 0){
                this.$message.error('请选择生产中的外协单号 !');
                return;
            }

            var self = this;
            this.$confirm('是否确认批量收货?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                callback: function (f) {
                    if (f == 'confirm'){
                        $.ajax({
                            type: "POST",
                            url: baseURL + "order/erporder/twbOrderSuppReceiveGoods",
                            contentType: "application/json",
                            data: JSON.stringify(productionIds),
                            success: function(r){
                                if(r.code === 0){
                                    self.query();
                                }else{
                                    alert(r.msg);
                                }
                            }
                        });
                    }
                }
            })
        },
        // 页数变化
        handleSizeChange: function (val) {
            this.page.pageSize = val;
            this.query();
        },
        // 当前页变化
        handleCurrentChange: function (val) {
            this.page.currentPage = val;
            this.query();
        },
        reset: function(){
            this.q.orderNo = null,
            this.q.producerName = null,
            this.q.createTime = null,
            this.q.beginCreatetime = null,
            this.q.endCreatetime = null,
            this.q.proStatus = 1,
            this.q.finStatus = 1
        },
        dateFormat: function(date) {
            var year = date.getFullYear();
            var month = date.getMonth()+1<10 ? "0"+(date.getMonth()+1) : date.getMonth()+1;
            var day = date.getDate()<10 ? "0"+date.getDate() : date.getDate();
            var hours = date.getHours()<10 ? "0"+date.getHours() : date.getHours();
            var minutes = date.getMinutes()<10 ? "0"+date.getMinutes() : date.getMinutes();
            var seconds = date.getSeconds()<10 ? "0"+date.getSeconds() : date.getSeconds();
            return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
        },
    }
});